<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class RuleController extends AdminBaseController{
    public function index(){
        $rule = M('rule');
        $list = $rule -> field('id,pid,title,name,status') ->  select();

        $list = getTree($list,0,0,'--');
        $this -> assign('list',$list);
        $this -> display();
    }
    public function add(){
        $pid = I('get.pid/d',0);
        $this->assign('pid',$pid);
        $this -> display();
    }
    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this -> redirect('Admin/Rule/index');
            }
            $rule = M('rule');
            $ruleInfo = $rule-> field('id,title,name')-> where(['id' => $id]) ->find();
            $this -> assign('ruleInfo',$ruleInfo);
            $this->display();
        }
    }
    public function save(){
        $id = I('post.id/d',0);
        if(empty($id)){
            if(IS_POST){
                $title = I('post.title/s','');
                $name = I('post.name/s','');
                $status = I('post.status/d',0);
                $pid = I('post.pid/d',0);
                $rule = M('rule');
                if(empty($title)){
                    $this->ajaxReturn(['msg' => '权限名称不能为空！','status' => '0']);
                }
                if(empty($name)){
                    $this->ajaxReturn(['msg' => '权限不能为空！','status' => '0']);
                }
                $ruleInfo = $rule -> where(['title' => $title,'fish' => 'OR','name' => $name])->find();
                if($ruleInfo){
                    $this->ajaxReturn(['msg' => '该权限已存在！','status' => '0']);
                }
                $data['pid'] = $pid;
                $data['title'] = $title;
                $data['name'] = $name;
                $data['status'] = $status;
                $result = $rule -> data($data) -> add();
                if(empty($result)){
                    $this->ajaxReturn(['msg' => '添加失败！','status' => '0']);
                }
                $this->ajaxReturn(['msg' => '添加成功！','status' => '1']);
            }else{
                $this->ajaxReturn(['msg' => '添加失败!','status' => '0']);
            }
        }else{
            if(IS_POST){
                $title = I('post.title/s','');
                $name = I('post.name/s','');
                $rule = M('rule');
                $data['title'] = $title;
                $data['name'] = $name;
                $result = $rule -> where(['id' => $id]) -> save($data);
                if(!empty($result)){
                    $this->ajaxReturn(['msg' => '修改成功！','status' => '1']);
                }
                $this->ajaxReturn(['msg' => '修改失败1!','status' => '0']);
            }else{
                $this->ajaxReturn(['msg' => '修改失败2!','status' => '0']);
            }
        }
    }
    public function ruleDel(){
        if(IS_POST){
            $id = I('post.id/d');
            if(($id != 1) && !empty($id)){
                $rule = M('rule');
                $map['id'] = $id;
                $result = $rule -> where($map) -> delete();
                if(!$result){
                    $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
                }
                $this->ajaxReturn([ 'title' => '删除成功！','msg' => '您已经永久删除了这条信息。','status' => 'success']);
            }else{
                $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
            }
        }else{
            $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
        }
    }

    //改变权限状态
    public function ruleChange(){
        if(IS_POST){
            $id = I('post.id/d');
            $rule = M('rule');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $data = $rule -> field('status') -> where($map) -> find();
                if(!empty($data)){
                    if($data['status'] == 1 ){
                        $change['status'] = 0;
                        $rule -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '禁用成功！','status' => 1,'state' => 0]);
                    }else{
                        $change['status'] = 1;
                        $rule -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '启用成功！','status' => 1,'state' => 1]);
                    }
                }else{
                    $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
                }
            }else{
                $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
            }

        }else{
            $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
        }
    }
}